package itext;

import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

public class PDFConverter {
	public static void createPDFfromTemplate(String templatePath,
			String outFilePath, HashMap<String, String> hmFields)
			throws Exception {
		// Reads in the pdf Template
		PdfReader reader = new PdfReader(templatePath);
		PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(
				outFilePath));
		AcroFields form = stamp.getAcroFields();

//		Set set = hmFields.entrySet();
//
//		Iterator i = set.iterator();
//
//		while (i.hasNext()) {
//			Map.Entry me = (Map.Entry) i.next();
//			if (null != me.getValue() && null != me.getKey()) {
//				form.setField(me.getKey().toString(), me.getValue().toString());
//			}
//		}
//		stamp.setFormFlattening(false);
		
		PdfDictionary pdfDict = (PdfDictionary) reader.getPdfObject(reader
				.getCatalog().get(PdfName.ACROFORM));
		pdfDict.remove(new PdfName("XFA"));
		stamp.close();

	}

	public static void main(String[] args) throws Exception {
		String templatePath = "D:\\webreach\\data\\pdf\\WebReach_budget_form.pdf";
		String outFilePath = "D:\\webreach\\data\\pdf\\abc.pdf";
		HashMap<String, String> hmFields = null;
		PDFConverter.createPDFfromTemplate(templatePath, outFilePath, hmFields);
	}
}
